/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.courier.view;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.sun.courier.DemoProperties;
import com.sun.courier.dal.GetStatusCourierDAL;
import com.sun.courier.entity.CourierManage;
import com.sun.courier.entity.Courier;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author quanbh_c00268
 */
@DemoProperties(
        value = "View Courier",
        category = "Get Status Courier",
        description = "Magage Courier Information, Including: Insert, Update and Delete",
        sourceFiles = { //                "com/sun/courier/view/EmployeeManagement.java",
//                "com/sun/courier/view/resources/images/EmployeeManagement.png",
})
public class GetStatusCourier extends JPanel {

    GetStatusCourierDAL dalCourier = new GetStatusCourierDAL();
    ArrayList<Courier> lstCourier;
    EditTablModel model;

    public GetStatusCourier() {
        initComponents();
        lstCourier = dalCourier.getAll();
        loadTable();
    }

    private void loadTable() {
        Vector cols = new Vector();
        cols.add("No");
        cols.add("Courier Name");
        cols.add("Customer Name");
        cols.add("Phone");
        cols.add("Status");
        cols.add("Weight");
        cols.add("Depart Location");
        cols.add("Destination");
        cols.add("Distance");
        cols.add("ReceivingCustomer");

        Vector data = new Vector();
        int i = 1;
        for (Courier item : lstCourier) {
            Vector row = new Vector();
            row.add(i++);
            row.add(item.getCourierName());
            row.add(item.getCustomerName());
            row.add(item.getPhone());
            row.add(item.getStatus());
            row.add(item.getWeight());
            row.add(item.getDepartLocation());
            row.add(item.getDestination());
            row.add(item.getDistance());
            row.add(item.getReceivingCustomer());           
            data.add(row);
        }

        //DefaultTableModel tblModel = new DefaultTableModel(data, cols);
        //tblItem.setModel(tblModel);
        model = new EditTablModel(data, cols);
        tblItem.setModel(model);
        

        tblItem.getColumnModel().getColumn(0).setMaxWidth(50);
        tblItem.getColumnModel().getColumn(0).setMinWidth(50);

        tblItem.getColumnModel().getColumn(4).setMaxWidth(180);
        tblItem.getColumnModel().getColumn(4).setMinWidth(180);

        tblItem.getColumnModel().getColumn(5).setMaxWidth(60);
        tblItem.getColumnModel().getColumn(5).setMinWidth(60);
        
        tblItem.getColumnModel().getColumn(8).setMaxWidth(60);
        tblItem.getColumnModel().getColumn(8).setMinWidth(60);
    }
    
    private class EditTablModel extends DefaultTableModel{
        boolean[] columnEditable;  
   
        public EditTablModel(Vector data, Vector columnNames) {  
            super(data, columnNames);  
            columnEditable = new boolean[columnNames.size()];  
            Arrays.fill(columnEditable, false);  
        }  
   
        public boolean isCellEditable(int row, int column) {  
            if (!columnEditable[column]) {  
                return false;  
            }  
            else {  
                return super.isCellEditable(row, column);  
            }  
        }  
   
        public void setColumnEditable(int column, boolean editable) {  
            columnEditable[column] = editable;  
        }  
   
        public boolean getColumnEditable(int column) {  
            return columnEditable[column];  
        }  
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txtSearch = new javax.swing.JTextField();
        cbSearch = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblItem = new javax.swing.JTable() {
            @Override
            public boolean isCellEditable(int row, int columns) {

                switch (columns) {
                    case 4:
                        return true;
                    default:
                        return false;
                }
            }
        };

        cbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Courier Name", "Customer Name", "Phone"}));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        tblItem.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
            {},
            {},
            {},
            {}
        },
                new String[]{}));
        tblItem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblItemMouseClicked(evt);
            }
        });

        tblItem.setRowHeight(30);
        jScrollPane2.setViewportView(tblItem);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane2)
                .addGroup(layout.createSequentialGroup()
                .addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)));
    }// </editor-fold>                        

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        String strSearch = txtSearch.getText();
        if (strSearch.isEmpty()) {
            lstCourier = dalCourier.getAll();
        } else {
            int intCB = cbSearch.getSelectedIndex();
            switch (intCB) {
                case 0:
                    lstCourier = dalCourier.searchCourierName(strSearch);
                    break;
                case 1:
                    lstCourier = dalCourier.searchCustemer(strSearch);
                    break;
                case 2:
                    lstCourier = dalCourier.searchPhone(strSearch);
                    break;
            }
        }
        loadTable();
    }

    private void tblItemMouseClicked(MouseEvent evt) {
        int row = tblItem.getSelectedRow();
        if(row>=0){
            ArrayList<CourierManage> entitys = dalCourier.getByIdStatus(lstCourier.get(row).getCourierId());
            DialogStatusCourier dlgCourier = new DialogStatusCourier(new JFrame(), true, entitys);
            dlgCourier.setLocation(350, 200);
            dlgCourier.setVisible(true);
        }
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbSearch;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblItem;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration       s
}
